﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SW.Lite.Infrastructure.Exception;

namespace SW.Lite.Infrastructure.Helpers
{
    public class SessionHelper
    {
        private static SessionHelper _instance;

        /// <summary>
        ///     Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static SessionHelper Instance
        {
            get { return _instance ?? (_instance = new SessionHelper()); }
        }

        public T GetSession<T>(string name)
        {
            var value = HttpContext.Current.Session[name];

            if (value == null)
                return default(T);

            try
            {
                return (T)value;
            }
            catch (System.Exception ex)
            {
                throw new CodeException(0x000101, "", ex);
            }
        }

        public T GetSession<T>(string name, T defValue)
        {
            var value = HttpContext.Current.Session[name];

            if (value == null)
                return defValue;

            try
            {
                return (T)value;
            }
            catch (System.Exception ex)
            {
                throw new CodeException(0x000101, "", ex);
            }
        }
    }
}
